package com.cyj.dream.core.util.pojo;

import com.cyj.dream.core.constant.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @Description: 自定义错误结果返回实体类--用于全局异常拦截
 * @BelongsProject: DreamChardonnay
 * @BelongsPackage: com.cyj.dream.core.util.pojo
 * @Author: ChenYongJia
 * @CreateTime: 2021-09-26 10:56
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Data
@ApiModel(value = "ErrorResult对象", description = "自定义错误结果返回实体类--用于全局异常拦截")
public class ErrorResult implements Serializable {

    private static final long serialVersionUID = 1L;


    @ApiModelProperty(value = "错误状态码")
    private Integer code;


    @ApiModelProperty(value = "错误信息")
    private String message;


    @ApiModelProperty(value = "异常信息")
    private String exception;

    /**
     * 返回错误信息
     *
     * @param resultCode
     * @param e
     * @param message
     * @return
     */
    public static ErrorResult fail(ResultCode resultCode, Throwable e, String message) {
        ErrorResult errorResult = ErrorResult.fail(resultCode, e);
        errorResult.setMessage(message);
        return errorResult;
    }

    /**
     * 返回统一api的信息并返回错误信息
     *
     * @param resultCode
     * @param e
     * @return
     */
    public static ErrorResult fail(ResultCode resultCode, Throwable e) {
        ErrorResult errorResult = new ErrorResult();
        errorResult.setCode(resultCode.code());
        errorResult.setMessage(resultCode.message());
        errorResult.setException(e.getClass().getName());
        return errorResult;
    }

    /**
     * 返回状态码和信息
     *
     * @param code
     * @param message
     * @return
     */
    public static ErrorResult fail(Integer code, String message) {
        ErrorResult errorResult = new ErrorResult();
        errorResult.setCode(code);
        errorResult.setMessage(message);
        return errorResult;
    }
}
